<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!DOCTYPE html>
<html>

	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title></title>
		<link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
		<!-- 引用Vue -->
		<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
		<!-- 引入组件库 -->
		<script src="https://unpkg.com/element-ui/lib/index.js"></script>
		<script type="text/javascript" src="../js/axios.js"></script>
		<script type="text/javascript" src="../js/qs.js"></script>
		<style>
			* {
				margin: 0;
				padding: 0;
			}
			
			.app {
				width: 750px;
				height: 500px;
				margin: 0 auto;
			}
			
			.border-t-a {
				margin-bottom: 20px;
			}
		</style>
	</head>

	<body>
		<div id="app" class="app">

			<hr class="border-t-a">

			<el-form :model="form" :rules="rules" ref="form" label-width="100px" class="demo-ruleForm">

				<el-form-item label="关怀主题：" prop="theme">
					<el-input v-model="form.theme"></el-input>
				</el-form-item>

				<el-form-item label="客户：">
					<el-col :span="12">
						<el-form-item prop="custname">
							<el-input v-model="form.custname" disabled></el-input>
						</el-form-item>
					</el-col>

					<el-col :span="12">
						<el-form-item label="执行人:" prop="linId">
							<el-select v-model="form.linName" @change="handleSelect1">
								<el-option v-for="item in customertoman" :key="item.linId" :label="item.linName" :value="item.linId">
							</el-select>
						</el-form-item>
					</el-col>
				</el-form-item>

				<el-form-item>
					<el-col :span="8">
						<el-autocomplete class="inline-input" v-model="form.custname" :fetch-suggestions="querySearch" placeholder="请输入客户名字" :trigger-on-focus="false" @select="handleSelect">
							<template slot-scope="{ item }">
								<div class="name">{{ item.cus_name }}({{item.cus_id}})</div>
							</template>
						</el-autocomplete>
					</el-col>
					<el-col class="line" :span="3" :offset="5">执行人：</el-col>
					<el-col :span="8">
						${username}
					</el-col>
				</el-form-item>

				<el-form-item label="类型：">
					<el-col :span="12">
						<el-form-item prop="type">
							<el-select v-model="form.type" placeholder="请选择类型">
								<el-option label="送礼物" :value="441"></el-option>
								<el-option label="发短信" :value="442"></el-option>
								<el-option label="打电话" :value="443"></el-option>
							</el-select>
						</el-form-item>
					</el-col>

					<el-col :span="12">
						<el-form-item label="日期:" prop="dates">
							<el-date-picker type="date" placeholder="选择日期" v-model="form.dates" format="yyyy-MM-dd" value-format="yyyy-MM-dd" :picker-options="pickerOptions1"></el-date-picker>
						</el-form-item>
					</el-col>
				</el-form-item>

				<el-form-item label="关怀内容：">
					<el-input type="textarea" autosize v-model="form.content"></el-input>
				</el-form-item>

				<el-form-item label="反馈：">
					<el-input type="textarea" autosize v-model="form.back"></el-input>
				</el-form-item>

				<el-form-item label="备注：">
					<el-input type="textarea" :autosize="{ minRows: 4, maxRows: 6}" v-model="form.more"></el-input>
				</el-form-item>

				<el-form-item>
					<el-button type="primary" @click="submitForm('form')">立即创建</el-button>
					<el-button @click="resetForm('form')">重置</el-button>
				</el-form-item>
			</el-form>
		</div>
		<script>
			var vms = new Vue({
				el: "#app",
				data() {
					return {
						customertoman: [],
						pickerOptions1: { //时间做约束
							disabledDate(time) {
								return time.getTime() < Date.now() - 8.64e7;
							}
						},
						form: {
							theme: '',
							custname: '',
							custid: '',
							linId: '',
							linName: '',
							dates: '',
							user: '',
							type: '',
							content: '',
							back: '',
							more: '',
							id: '',
							restaurants: []
						},
						//验证规则
						rules: { //数据源model
							theme: [{
								required: true,
								message: '请输入关怀主题',
								trigger: 'blur'
							}],
							custname: [{
								required: true,
								message: '请选择客户',
								trigger: 'blur'
							}],
							linName: [{
								required: true,
								message: '请选择联系人',
								trigger: 'blur'
							}],
							dates: [{
								required: true,
								message: '请选择时间',
								trigger: 'blur'
							}],
							user: [{
								required: true,
								message: '请选择执行人',
								trigger: 'blur'
							}],
							type: [{
								required: true,
								message: '请选择类型',
								trigger: 'blur'
							}]
						}

					}
				},
				methods: {
					submitForm(formName) {
						//验证方法
						this.$refs[formName].validate((valid) => {
							if(valid) {
								var par = null;
								if(JSON.stringify(${
										care
									}) === '{}') {
									par = "/careform";
								} else {
									par = "/editercareform";
									this.form.id = ${
										care.car_id
									} + "";
								}
								//post提交数据
								axios.post(par,
									//参数转换
									Qs.stringify(this.form, {
										arrayFormat: 'brackets'
									})
									//	params:
									//ajax请求成功的回调函数
								).then(function(v) {
									window.parent.location.href = "/getCarety";
								}).catch(function() {
									alert("出错了");
								});

							} else {
								console.log('error submit!!');
								return false;
							}
						});
					},
					resetForm(formName) {
						this.$refs[formName].resetFields();
					},

					querySearch: function(queryString, cb) {
						var allarr = this.form.restaurants;
						var newsarr = [];
						allarr.forEach(v => {
							var strs = v.cus_name + "";
							if(strs.indexOf(queryString) >= 0) {
								newsarr.push(v);
								console.info(strs);
								console.info(v);
							}
						})
						// 调用 callback 返回建议列表的数据
						cb(newsarr);
					},
					handleSelect: function(it) {
						console.log(it.cus_name);
						this.form.custname = it.cus_name;
						this.form.custid = it.cus_id;
						var iosnn = this;
						//post提交数据
						axios.post("/onSale/findByCoustomerName",
							//参数转换
							Qs.stringify({
								'custid': this.form.custid
							}, {
								arrayFormat: 'brackets'
							})
						).then(function(v) {
							iosnn.customertoman = v.data.custo;
							console.info(v.data);
						}).catch(function(e) {
							alert("出错了" + e);
						});

					},
					handleSelect1: function(it) {
						this.form.linId = it;

					},
				},
				mounted() {
					var dic = ${
						dictionaryitem
					};

					//console.log(dic); 

					var mas = ${
						mansitem
					};

					this.form.restaurants = mas;

					//console.log(this.form.restaurants); 
					var news = ${
						care
					};
					if(JSON.stringify(news) != '{}') {
						this.form.theme = news.car_name;
						this.form.custid = news.cus_id;
						this.form.custname = news.cus_name;
						this.form.linId = news.lin_id;
						this.form.linName = news.lin_name;
						this.form.dates = news.finishtime;

						this.form.user = news.use_name;
						this.form.type = news.car_type;
						this.form.mode = news.ser_mode;
						this.form.content = news.car_content;
						this.form.back = news.car_feedback;
						this.form.more = news.car_remark;
					}
				}
			});
		</script>
	</body>

</html>